﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;

using CharityGamesOnline.Modules.Game;
using CharityGamesOnline.Modules.Charity;
using CharityGamesOnline.Modules.Event;
using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Game.Trivia
{
    public partial class TriviaFlash : CGOBase
    {
        private int gameEnrollmentID;
        private int eventID;
        private string charityName;
        private const string flashFilePathFormat = "{0}?portalID={1}&enrollID={2}&userID={3}&EventID={4}&CharityName={5}&Webservice={6}?WSDL";
        private const string flashTryFilePathFormat = "{0}?PracticeGame=1&PracticeDataURL={1}&CharityName={2}";
        private const string webServiceFormat = "http://{0}{1}{2}";
        private const string defaultWebServiceFile = "Trivia.asmx";
        private const string defaultFlashFile = "CGO_TriviaChallenge.swf";
        private const string defaultPracticeDataFile = "PracticeGameData.xml";

        public int GameEnrollmentID
        {
            get
            {
                if (gameEnrollmentID == 0)
                {
                    if (Request.QueryString["GameEnrollmentID"] != null)
                    {
                        gameEnrollmentID = Int32.Parse(Request.QueryString["GameEnrollmentID"]);
                    }
                }
                return gameEnrollmentID;
            }
        }

        public bool TryGame
        {
            get
            {
                if (Request.QueryString["TryGame"] != null)
                {
                    return Boolean.Parse(Request.QueryString["TryGame"]);
                }
                else
                    return false;
            }
        }

        public int EventID
        {
            get
            {
                if (eventID == 0)
                {
                    GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
                    GameEnrollmentInfo objEnroll = ctrlEnroll.GetEnrollment(PortalId, GameEnrollmentID);
                    if (objEnroll != null)
                    {
                        eventID = objEnroll.EventID;
                    }
                }
                return eventID; 
            }
        }

        public string CharityName
        {
            get
            {
                if (string.IsNullOrEmpty(charityName))
                {                    
                    CharityController ctrlCharity = new CharityController();
                    List<CharityInfo> lstCharity = ctrlCharity.GetCharities(PortalId,EventID);
                    if (lstCharity != null && lstCharity.Count>0)
                    {
                        charityName = lstCharity[0].Name; 
                    } 
                }
                return charityName;
            }
        }

        public string FlashFile
        {
            get
            {
                if (!Null.IsNull(Settings["TriviaFlashFile"]))
                {
                    return ResolveUrl("Flash\\" + Settings["TriviaFlashFile"]);
                }
                else
                    return ResolveUrl("Flash\\" + defaultFlashFile);
            }
        }

        public string PracticeDataFile
        {
            get
            {
                if (!Null.IsNull(Settings["PracticeData"]))
                {
                    return ResolveUrl("Flash\\" + Settings["PracticeData"]);
                }
                else
                    return ResolveUrl("Flash\\" + defaultPracticeDataFile);                 
            }
        }

        private string TriviaWebServiceUrl
        {
            get
            {
                if (!Null.IsNull(Settings["TriviaWebServiceUrl"]))
                {
                    return string.Format(webServiceFormat, PortalAlias.HTTPAlias, ControlPath, Settings["TriviaWebServiceUrl"].ToString());
                    //return string.Format(webServiceFormat, ControlPath, Settings["TriviaWebServiceUrl"].ToString());
                }
                else
                {
                    return string.Format(webServiceFormat, PortalAlias.HTTPAlias, ControlPath, defaultWebServiceFile);
                    //return string.Format(webServiceFormat, ControlPath, defaultWebServiceFile);
                }
            }
        }

        public string FlashGame
        {
            get
            {
                if (TryGame)
                {
                    return string.Format(flashTryFilePathFormat, FlashFile, Server.HtmlEncode(PracticeDataFile), Server.HtmlEncode(CharityName));
                }
                else
                {
                    return string.Format(flashFilePathFormat, FlashFile, PortalId, GameEnrollmentID, UserId, EventID, Server.HtmlEncode(CharityName), Server.HtmlEncode(TriviaWebServiceUrl));
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //InitialScript();
        }

        private void InitialScript()
        {
            string csName = string.Empty;
            string csText = string.Empty;
            Type csType = System.Reflection.MethodBase.GetCurrentMethod().GetType();

            csName = "swfobjectScriptFile";
            csText = "<script src=\"" + ResolveUrl("swfobject.js") + "\" type=\"text/javascript\"></script>";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(csName))
            {
                Page.ClientScript.RegisterClientScriptBlock(csType, csName, csText);
            }

            csName = "triviaScriptFile";
            csText = "<script src=\"" + ResolveUrl("Trivia.js") + "\" type=\"text/javascript\"></script>";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(csName))
            {
                Page.ClientScript.RegisterStartupScript(csType, csName, csText);
            }

        }
    }
}